//
//  ViewController.m
//  Health
//
//  Created by LeeJay on 16/8/24.
//  Copyright © 2016年 LeeJay. All rights reserved.
//

#import "ViewController.h"
#import "ProfileInfoViewController.h"
#import <HealthKit/HealthKit.h>
#import "StepCountViewController.h"
#import "SleepAnalysisViewController.h"

@interface ViewController ()

@property (nonatomic, copy) NSArray *datas;
@property (nonatomic, strong) HKHealthStore *healthStore;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.datas = @[@"Profile Info",@"Step Count",@"Sleep Analysis"];
    self.healthStore = [[HKHealthStore alloc] init];
    
    if ([HKHealthStore isHealthDataAvailable]) {
        
        NSSet *writeDataTypes = [self dataTypesToWrite];
        NSSet *readDataTypes = [self dataTypesToRead];
        
        [self.healthStore requestAuthorizationToShareTypes:writeDataTypes
                                                 readTypes:readDataTypes
                                                completion:^(BOOL success, NSError * _Nullable error) {
            
            if (!success) {
                NSLog(@"%@",error);
                return;
            }
            
        }];
    }
}

/**
 要写入的数据
 */
- (NSSet *)dataTypesToWrite
{
    // 步数
    HKQuantityType *stepType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    // 体重
    HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
    // 身高
    HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
    // 睡眠分析
    HKCategoryType *sleepAnalysisType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
    
    return [NSSet setWithObjects:stepType, weightType, heightType, sleepAnalysisType, nil];
}

/**
 要读取的数据
 */
- (NSSet *)dataTypesToRead
{
    // 步数
    HKQuantityType *stepType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    // 体重
    HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
    // 身高
    HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
    // 生日
    HKCharacteristicType *birthdayType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth];
    // 睡眠分析
    HKCategoryType *sleepAnalysisType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
    
    return [NSSet setWithObjects:stepType, weightType, heightType, birthdayType, sleepAnalysisType, nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0)
    {
        ProfileInfoViewController *profileInfo = [[ProfileInfoViewController alloc] init];
        profileInfo.title = self.datas[indexPath.row];
        profileInfo.healthStore = self.healthStore;
        [self.navigationController pushViewController:profileInfo animated:YES];
    }
    else if (indexPath.row == 1)
    {
        StepCountViewController *temp = [[StepCountViewController alloc] init];
        temp.title = self.datas[indexPath.row];
        temp.healthStore = self.healthStore;
        [self.navigationController pushViewController:temp animated:YES];
    } else {
        SleepAnalysisViewController *sleep = [[SleepAnalysisViewController alloc] init];
        sleep.title = self.datas[indexPath.row];
        sleep.healthStore = self.healthStore;
        [self.navigationController pushViewController:sleep animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
